import {
	orderList, orderPaidList, orderSeatConsumed, orderSeatUnlock
} from '@/api/sr/seatOrder'
import {
	toast
} from '@/utils/common'
export default {
	data() {
		return {
			orders: [
				[],
				[]
			],
			listQuery: {
				pageNum: 1,
				pageSize: 20,
				orderByColumn: 'id',
				isAsc: 'descending'
			},
			list: [{
					name: '购买记录'
				},
				{
					name: '学习记录'
				}
			],
			current: 0,
			swiperCurrent: 0,
			loadStatus: ['loadmore', 'loadmore'],
		};
	},
	onShow(){
		let isFlag = this.checkLogin('/pages/order/orderlist')
		console.info('onshow', isFlag)
		if (isFlag) {
			//购买记录
			this.getOrders(1);
			//学习记录
			this.getOrders(2);
		}	
	},
	onLoad() {
	},
	computed: {
		// 价格小数
		priceDecimal() {
			return val => {
				if (val !== parseInt(val)) {
					return val.slice(-2)
				} else {
					return '00'
				}
			};
		},
		// 价格整数
		priceInt() {
			return val => {
				if (val !== parseInt(val)) return val.split('.')[0]
				else return val
			};
		}

	},
	methods: {
		reachBottom() {
			const current = this.current
			if (this.loadStatus[current] == 'nomore') {
				return
			}
			this.listQuery.pageNum++
			this.getOrders(current + 1)
		},
		getOrders(status) {
			this.orders[status - 1] = new Array()
			this.loadStatus.splice(status - 1, 1, "loading")
			if (status == 1) {
				orderList(this.listQuery).then(res => {
					let orderList = res.data.rows
					let total = res.data.total
					for (let index in orderList) {
						let order = orderList[index]
						if(order.priceType === 0){
							order.times = order.times + '小时'
						}else if(order.priceType === 1){
							order.times = order.times + '天'
						}
						this.orders[status - 1].push(order)
					}
					if (orderList.length === total) {
						this.loadStatus.splice(status - 1, 1, "nomore");
					} else {
						this.loadStatus.splice(status - 1, 1, "more");
					}
				}).catch((err) => {});
			} else {
				orderPaidList(this.listQuery).then(res => {
					let orderList = res.data.rows
					let total = res.data.total
					for (let index in orderList) {
						let order = orderList[index]
						if(order.priceType === 0){
							order.times = order.times + '小时'
						}else if(order.priceType === 1){
							order.times = order.times + '天'
						}
						this.orders[status - 1].push(order)
					}
					if (orderList.length === total) {
						this.loadStatus.splice(status - 1, 1, "nomore");
					} else {
						this.loadStatus.splice(status - 1, 1, "more");
					}
				}).catch((err) => {});
			}

		},
		formatPrice(val) {
			return (val / 100).toFixed(2)
		},
		// 总价
		totalPrice(item) {
			let price = 0
			item.map(val => {
				price += val.price
			});
			return this.formatPrice(price)
		},
		// tab栏切换
		change(item) {
			console.info("change", item)
			this.swiperCurrent = item.index
			this.current = item.index
			console.info("change", this.current, this.orders[this.current])
		},
		toDetail(orderSn) {
			this.$u.route({
				url: '/pages/order/detail',
				params: {
					orderSn: orderSn
				}
			})
		},
		orderSeatConsumed(orderId){
			var that = this;
			console.log('开锁', orderId)
			that.$modal.loading("正在开锁，请耐心等待...")
			orderSeatConsumed(orderId).then(response => {
				that.$modal.closeLoading()
				uni.showToast({
					title: '开锁成功！',
					icon: 'none'
				});
				//购买记录
				this.getOrders(1);
				//学习记录
				this.getOrders(2);
			}).catch(errors => {})
		},
		orderSeatUnlock(orderId) {
			var that = this;
			console.log('开锁', orderId)
			that.$modal.loading("正在开锁，请耐心等待...")
			orderSeatUnlock(orderId).then(response => {
				that.$modal.closeLoading()
				uni.showToast({
					title: '开锁成功！',
					icon: 'none'
				});
			}).catch(errors => {
				
			})
		},
		pay(orderNo, totalPrice) {
			this.$u.route({
				url: '/pages/order/payment/payment',
				params: {
					orderSn: orderNo,
					totalPrice: totalPrice
				}
			})
		},
	}
};